#include "stdafx.h"
#include "settings.h"
#include "Crypto.h"

using namespace MFC;

AccountSettings accountSettings;
BOOL firstRun;

AccountSettings::AccountSettings()
{
	logFile.Format("%s.log", _GLOBAL_NAME);
	iniFile.Format("%s.ini", _GLOBAL_NAME);

	CFileStatus rStatus;
	CString str;
	CString appData;
	LPTSTR ptr;
	ptr = appData.GetBuffer(MAX_PATH);
	::GetCurrentDirectory(MAX_PATH, ptr);
	appData.ReleaseBuffer();
	appData += "\\";

	CString localAppData;
	ptr = localAppData.GetBuffer(MAX_PATH);
	SHGetSpecialFolderPath(
		0,
		ptr, 
		CSIDL_LOCAL_APPDATA, 
		FALSE ); 
	localAppData.ReleaseBuffer();
	localAppData.AppendFormat("\\%s\\",_GLOBAL_NAME);

	firstRun = TRUE;
	if (CFile::GetStatus(appData + iniFile, rStatus)) {
		firstRun = FALSE;
	}

	HANDLE h = CreateFile( appData + iniFile, GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_READ |
		FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
	if (h != INVALID_HANDLE_VALUE )
	{
		LARGE_INTEGER filesize;
		if (::GetFileSizeEx(h, &filesize) && filesize.LowPart==0 ) {
			CloseHandle(h);
			CopyFile(localAppData + iniFile, appData + iniFile,  FALSE);
		} else {
			CloseHandle(h);
		}
		logFile = appData + logFile;
		iniFile = appData + iniFile;
	} else {
		CreateDirectory(localAppData, NULL);
		if (CFile::GetStatus(localAppData + iniFile, rStatus)) {
			firstRun = FALSE;
		}
		logFile = localAppData + logFile;
		iniFile = localAppData + iniFile;
	}

#ifndef _GLOBAL_ACCOUNT_MINI
	ptr = publicAddr.GetBuffer(255);
	GetPrivateProfileString("Settings","PublicAddr", NULL, ptr, 256, iniFile);
	publicAddr.ReleaseBuffer();
	ptr = listenPort.GetBuffer(255);
	GetPrivateProfileString("Settings","ListenPort", NULL, ptr, 256, iniFile);
	listenPort.ReleaseBuffer();
#endif


#ifdef _GLOBAL_ACCOUNT_SIP_SERVER
	server = _GLOBAL_ACCOUNT_SIP_SERVER;
#else
	ptr = server.GetBuffer(255);
	GetPrivateProfileString("Settings","Server", NULL, ptr, 256, iniFile);
	server.ReleaseBuffer();
#endif

#ifdef _GLOBAL_ACCOUNT_SIP_PROXY
	proxy = _GLOBAL_ACCOUNT_SIP_PROXY;
#else
	ptr = proxy.GetBuffer(255);
	GetPrivateProfileString("Settings","Proxy", NULL, ptr, 256, iniFile);
	proxy.ReleaseBuffer();
#endif

#ifdef _GLOBAL_ACCOUNT_SRTP
	srtp = _GLOBAL_ACCOUNT_SRTP;
#else
	ptr = srtp.GetBuffer(255);
	GetPrivateProfileString("Settings","SRTP", NULL, ptr, 256, iniFile);
	srtp.ReleaseBuffer();
#endif

#ifdef _GLOBAL_ACCOUNT_TRANSPORT
	transport = _GLOBAL_ACCOUNT_TRANSPORT;
#else
#ifndef _GLOBAL_ACCOUNT_MINI
	ptr = transport.GetBuffer(255);
	GetPrivateProfileString("Settings","Transport", NULL, ptr, 256, iniFile);
	transport.ReleaseBuffer();
#else
	transport="udp";
#endif
#endif

#ifdef _GLOBAL_ACCOUNT_PUBLISH
	publish=_GLOBAL_ACCOUNT_PUBLISH;
#else
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","Publish", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	publish=str=="1"?1:0;
#endif

#ifdef _GLOBAL_ACCOUNT_STUN
	stun = _GLOBAL_ACCOUNT_STUN;
#else
	ptr = stun.GetBuffer(255);
	GetPrivateProfileString("Settings","STUN", NULL, ptr, 256, iniFile);
	stun.ReleaseBuffer();
#endif

#ifdef _GLOBAL_ACCOUNT_ICE
	ice=_GLOBAL_ACCOUNT_ICE;
#else
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","ICE", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	ice=str=="1"?1:0;
#endif

#ifdef _GLOBAL_ACCOUNT_ALLOW_REWRITE
	allowRewrite = _GLOBAL_ACCOUNT_ALLOW_REWRITE;
#else
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","allowRewrite", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	allowRewrite=str=="1"?1:0;
#endif


#if !defined _GLOBAL_ACCOUNT_DOMAIN || defined _GLOBAL_PROFILE
	ptr = domain.GetBuffer(255);
	GetPrivateProfileString("Settings","Domain", NULL, ptr, 256, iniFile);
	domain.ReleaseBuffer();
#ifdef _GLOBAL_PROFILE
	if (domain=="B") {
		domain = _GLOBAL_PROFILE_B;
#ifdef _GLOBAL_PROFILE_C
	} else if (domain=="C") {
		domain = _GLOBAL_PROFILE_C;
#endif
	} else {
		domain = _GLOBAL_PROFILE_A;
	}
	server=domain;
	proxy=domain;
#endif
#else
	domain= _GLOBAL_ACCOUNT_DOMAIN;
#endif

#ifdef _GLOBAL_ACCOUNT_LOGIN
	authID= _GLOBAL_ACCOUNT_LOGIN;
#else
	ptr = authID.GetBuffer(255);
	GetPrivateProfileString("Settings","AuthID", NULL, ptr, 256, iniFile);
	authID.ReleaseBuffer();
#endif

	CString usernameLocal;
	CString passwordLocal;
#ifdef _GLOBAL_ACCOUNT_USERNAME
	usernameLocal = _GLOBAL_ACCOUNT_USERNAME;
#else
	ptr = usernameLocal.GetBuffer(255);
	GetPrivateProfileString("Settings","Username", NULL, ptr, 256, iniFile);
	usernameLocal.ReleaseBuffer();
#endif

#ifdef _GLOBAL_ACCOUNT_PASSWORD
	passwordLocal = _GLOBAL_ACCOUNT_PASSWORD;
#else
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","PasswordSize", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	if (str!="") {
		CByteArray arPassword;
		int size = atoi(str);
		arPassword.SetSize(size>0?size:16);
		GetPrivateProfileStruct("Settings", "Password", arPassword.GetData(),arPassword.GetSize(), iniFile);
		CCrypto crypto;
		if (crypto.DeriveKey(_GLOBAL_KEY)) {
			crypto.Decrypt(arPassword,passwordLocal);
		}
	} else {
		ptr = passwordLocal.GetBuffer(255);
		GetPrivateProfileString("Settings","Password", NULL, ptr, 256, iniFile);
		passwordLocal.ReleaseBuffer();
	}
#endif

#ifndef _GLOBAL_ACCOUNT_PASSWORD
#ifndef _GLOBAL_ACCOUNT_REMEMBER_PASSWORD_CHECKED
	rememberPassword = passwordLocal.GetLength()?1:0;
#else
	rememberPassword = !usernameLocal.GetLength() || passwordLocal.GetLength()?1:0;
#endif
#else
	rememberPassword = usernameLocal.GetLength()?1:0;
#endif


#ifndef _GLOBAL_API
	username = usernameLocal;
	password = passwordLocal;
#else
	username = "";
	password = "";
	apiLogin = usernameLocal;
	apiPassword = passwordLocal;
#endif

#ifdef _GLOBAL_ACCOUNT_NAME
	displayName = _GLOBAL_ACCOUNT_NAME;
#else
	ptr = displayName.GetBuffer(255);
	GetPrivateProfileString("Settings","DisplayName", NULL, ptr, 256, iniFile);
	displayName.ReleaseBuffer();
#endif

#if !defined _GLOBAL_CUSTOM || defined _GLOBAL_UPDATES
	ptr = updatesInterval.GetBuffer(255);
	GetPrivateProfileString("Settings","UpdatesInterval", NULL, ptr, 256, iniFile);
	updatesInterval.ReleaseBuffer();
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","CheckUpdatesTime", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	checkUpdatesTime = atoi(str);
#endif

#ifndef _GLOBAL_ACCOUNT_MINI
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","disableLocalAccount", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	disableLocalAccount=str=="1"?1:0;
#else
	disableLocalAccount=1;
#endif

#ifndef _GLOBAL_NO_AUTO
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","AutoAnswer", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
#ifdef _GLOBAL_AUTO_ANSWER_VALUE
	autoAnswer=str=="1"?1:(str=="2"?2:(str=="0"?0:_GLOBAL_AUTO_ANSWER_VALUE));
#else
	autoAnswer=str=="1"?1:(str=="2"?2:0);
#endif
#endif

	ptr = userAgent.GetBuffer(255);
	GetPrivateProfileString("Settings","userAgent", NULL, ptr, 256, iniFile);
	userAgent.ReleaseBuffer();

	ptr = denyIncoming.GetBuffer(255);
	GetPrivateProfileString("Settings","denyIncoming", NULL, ptr, 256, iniFile);
	denyIncoming.ReleaseBuffer();

#ifndef _GLOBAL_NO_CONTACTS
	ptr = usersDirectory.GetBuffer(255);
	GetPrivateProfileString("Settings","usersDirectory", NULL, ptr, 256, iniFile);
	usersDirectory.ReleaseBuffer();
#endif

#ifndef _GLOBAL_SOUND_EVENTS
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","LocalDTMF", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
#ifdef _GLOBAL_SOUND_EVENTS_VALUE
	localDTMF=str=="0"?0:(str=="1"?1:_GLOBAL_SOUND_EVENTS_VALUE);
#else
	localDTMF=str=="0"?0:1;
#endif
#else
	localDTMF = _GLOBAL_SOUND_EVENTS;
#endif

	ptr = ringingSound.GetBuffer(255);
	GetPrivateProfileString("Settings","RingingSound", NULL, ptr, 256, iniFile);
	ringingSound.ReleaseBuffer();
	ptr = audioInputDevice.GetBuffer(255);
	GetPrivateProfileString("Settings","AudioInputDevice", NULL, ptr, 256, iniFile);
	audioInputDevice.ReleaseBuffer();
	ptr = audioOutputDevice.GetBuffer(255);
	GetPrivateProfileString("Settings","AudioOutputDevice", NULL, ptr, 256, iniFile);
	audioOutputDevice.ReleaseBuffer();
	ptr = audioRingDevice.GetBuffer(255);
	GetPrivateProfileString("Settings","AudioRingDevice", NULL, ptr, 256, iniFile);
	audioRingDevice.ReleaseBuffer();
	
#ifndef _GLOBAL_CODECS_HARDCODED
	ptr = audioCodecs.GetBuffer(255);
	GetPrivateProfileString("Settings","AudioCodecs", NULL, ptr, 256, iniFile);
	audioCodecs.ReleaseBuffer();
#else
	audioCodecs = _GLOBAL_CODECS_HARDCODED;
#endif

	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","DisableVAD", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	disableVAD = str == "0" ? 0 : 1;

#ifdef _GLOBAL_VIDEO
	ptr = videoCaptureDevice.GetBuffer(255);
	GetPrivateProfileString("Settings","VideoCaptureDevice", NULL, ptr, 256, iniFile);
	videoCaptureDevice.ReleaseBuffer();
	ptr = videoCodec.GetBuffer(255);
	GetPrivateProfileString("Settings","VideoCodec", NULL, ptr, 256, iniFile);
	videoCodec.ReleaseBuffer();
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","DisableH264", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	disableH264 = str == "1" ? 1 : 0;
	ptr = bitrateH264.GetBuffer(255);
	GetPrivateProfileString("Settings","BitrateH264", NULL, ptr, 256, iniFile);
	bitrateH264.ReleaseBuffer();
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","DisableH263", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	disableH263 = str == "1" ? 1 : 0;
	ptr = bitrateH263.GetBuffer(255);
	GetPrivateProfileString("Settings","BitrateH263", NULL, ptr, 256, iniFile);
	bitrateH263.ReleaseBuffer();
#endif

	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","mainX", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	mainX = atoi(str);
	
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","mainY", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	mainY = atoi(str);

	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","mainW", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	mainW = atoi(str);
	
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","mainH", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	mainH = atoi(str);

	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","messagesX", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	messagesX = atoi(str);
	
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","messagesY", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	messagesY = atoi(str);
	
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","messagesW", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	messagesW = atoi(str);
	
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","messagesH", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	messagesH = atoi(str);

	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","callsWidth0", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	callsWidth0 = atoi(str);

	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","callsWidth1", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	callsWidth1 = atoi(str);

	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","callsWidth2", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	callsWidth2 = atoi(str);

	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","callsWidth3", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	callsWidth3 = atoi(str);

	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","callsWidth4", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	callsWidth4 = atoi(str);

	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","ActiveTab", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	activeTab = atoi(str);

	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","AlwaysOnTop", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	alwaysOnTop = atoi(str);

	ptr = cmdIncomingCall.GetBuffer(255);
	GetPrivateProfileString("Settings","cmdIncomingCall", NULL, ptr, 256, iniFile);
	cmdIncomingCall.ReleaseBuffer();

	ptr = cmdCallAnswer.GetBuffer(255);
	GetPrivateProfileString("Settings","cmdCallAnswer", NULL, ptr, 256, iniFile);
	cmdCallAnswer.ReleaseBuffer();

#ifndef _GLOBAL_NO_LOG
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","EnableLog", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	enableLog=str=="1"?1:0;
#else
	enableLog=0;
#endif

#ifndef _GLOBAL_SINGLE_MODE
	ptr = str.GetBuffer(255);
	GetPrivateProfileString("Settings","SingleMode", NULL, ptr, 256, iniFile);
	str.ReleaseBuffer();
	singleMode=str=="1"?1:0;
#else
	singleMode = 1;
#endif

	hidden = 0;
}

void AccountSettings::Save()
{
	CString str;

#ifndef _GLOBAL_ACCOUNT_SIP_SERVER
		WritePrivateProfileString("Settings","Server",server,iniFile);
#endif

#ifndef _GLOBAL_ACCOUNT_SIP_PROXY
	WritePrivateProfileString("Settings","Proxy",proxy,iniFile);
#endif

#ifdef _GLOBAL_PROFILE
	if (domain == _GLOBAL_PROFILE_B) {
		WritePrivateProfileString("Settings","Domain","B",iniFile);
#ifdef _GLOBAL_PROFILE_C
	} else if (domain == _GLOBAL_PROFILE_C) {
		WritePrivateProfileString("Settings","Domain","C",iniFile);
#endif
	} else {
		WritePrivateProfileString("Settings","Domain","A",iniFile);
	}
#else
#ifndef _GLOBAL_ACCOUNT_DOMAIN
	WritePrivateProfileString("Settings","Domain",domain,iniFile);
#endif
#endif

#ifndef _GLOBAL_ACCOUNT_LOGIN
	WritePrivateProfileString("Settings","AuthID",authID,iniFile);
#endif

	CString usernameLocal;
	CString passwordLocal;

#ifndef _GLOBAL_API
	usernameLocal = username;
	passwordLocal = password;
#else
	usernameLocal = apiLogin;
	passwordLocal = apiPassword;
#endif

	CCrypto crypto;
	CByteArray arPassword;
	if (crypto.DeriveKey(_GLOBAL_KEY)) {
		crypto.Encrypt(passwordLocal,arPassword);
	}

	if (!rememberPassword) {
#ifndef _GLOBAL_ACCOUNT_USERNAME
		WritePrivateProfileString("Settings","Username","",iniFile);
#endif
#ifndef _GLOBAL_ACCOUNT_PASSWORD
		WritePrivateProfileString("Settings","Password","",iniFile);
		WritePrivateProfileString("Settings","PasswordSize",NULL,iniFile);
#endif
	}

	if (rememberPassword) {
#ifndef _GLOBAL_ACCOUNT_USERNAME
		WritePrivateProfileString("Settings","Username",usernameLocal,iniFile);
#endif
#ifndef _GLOBAL_ACCOUNT_PASSWORD
		if (arPassword.GetSize()) {
			WritePrivateProfileStruct("Settings","Password",arPassword.GetData(),arPassword.GetSize(),iniFile);
			CString str;
			str.Format("%d",arPassword.GetSize());
			WritePrivateProfileString("Settings","PasswordSize",str,iniFile);
		} else {
			WritePrivateProfileString("Settings","Password",passwordLocal,iniFile);
			WritePrivateProfileString("Settings","PasswordSize",NULL,iniFile);
		}
#endif
	}

#ifndef _GLOBAL_ACCOUNT_NAME
	WritePrivateProfileString("Settings","DisplayName",displayName,iniFile);
#endif

#ifndef _GLOBAL_ACCOUNT_MINI
	WritePrivateProfileString("Settings","PublicAddr",publicAddr,iniFile);
	WritePrivateProfileString("Settings","ListenPort",listenPort,iniFile);
	WritePrivateProfileString("Settings","SRTP",srtp,iniFile);
	WritePrivateProfileString("Settings","Transport",transport,iniFile);
	WritePrivateProfileString("Settings","Publish",publish?"1":"0",iniFile);
	WritePrivateProfileString("Settings","STUN",stun,iniFile);
	WritePrivateProfileString("Settings","ICE",ice?"1":"0",iniFile);
	WritePrivateProfileString("Settings","allowRewrite",allowRewrite?"1":"0",iniFile);
	WritePrivateProfileString("Settings","disableLocalAccount",disableLocalAccount?"1":"0",iniFile);
#endif

#ifndef _GLOBAL_NO_LOG
	WritePrivateProfileString("Settings","EnableLog",enableLog?"1":"0",iniFile);
#endif

#if !defined _GLOBAL_CUSTOM || defined _GLOBAL_UPDATES
	WritePrivateProfileString("Settings","UpdatesInterval",updatesInterval,iniFile);
	str.Format("%d",checkUpdatesTime);
	WritePrivateProfileString("Settings","CheckUpdatesTime",str,iniFile);
#endif

#ifndef _GLOBAL_NO_AUTO
	WritePrivateProfileString("Settings","AutoAnswer",autoAnswer==1?"1":(autoAnswer==2?"2":"0"),iniFile);
#endif
	WritePrivateProfileString("Settings","denyIncoming",denyIncoming,iniFile);
#ifndef _GLOBAL_NO_CONTACTS
	WritePrivateProfileString("Settings","usersDirectory",usersDirectory,iniFile);
#endif
#ifndef _GLOBAL_SINGLE_MODE
	WritePrivateProfileString("Settings","SingleMode",singleMode?"1":"0",iniFile);
#endif
#ifndef _GLOBAL_SOUND_EVENTS
	WritePrivateProfileString("Settings","LocalDTMF",localDTMF?"1":"0",iniFile);
#endif
	WritePrivateProfileString("Settings","RingingSound",ringingSound,iniFile);
	WritePrivateProfileString("Settings","AudioInputDevice","\""+audioInputDevice+"\"",iniFile);
	WritePrivateProfileString("Settings","AudioOutputDevice","\""+audioOutputDevice+"\"",iniFile);
	WritePrivateProfileString("Settings","AudioRingDevice","\""+audioRingDevice+"\"",iniFile);
#ifndef _GLOBAL_CODECS_HARDCODED
	WritePrivateProfileString("Settings","AudioCodecs",audioCodecs,iniFile);
#endif
	WritePrivateProfileString("Settings","DisableVAD",disableVAD?"1":"0",iniFile);

#ifdef _GLOBAL_VIDEO
	WritePrivateProfileString("Settings","VideoCaptureDevice","\""+videoCaptureDevice+"\"",iniFile);
	WritePrivateProfileString("Settings","VideoCodec",videoCodec,iniFile);
	WritePrivateProfileString("Settings","DisableH264",disableH264?"1":"0",iniFile);
	WritePrivateProfileString("Settings","BitrateH264",bitrateH264,iniFile);
	WritePrivateProfileString("Settings","DisableH263",disableH263?"1":"0",iniFile);
	WritePrivateProfileString("Settings","BitrateH263",bitrateH263,iniFile);
#endif

	str.Format("%d",mainX);
	WritePrivateProfileString("Settings","mainX",str,iniFile);

	str.Format("%d",mainY);
	WritePrivateProfileString("Settings","mainY",str,iniFile);

	str.Format("%d",mainW);
	WritePrivateProfileString("Settings","mainW",str,iniFile);

	str.Format("%d",mainH);
	WritePrivateProfileString("Settings","mainH",str,iniFile);

	str.Format("%d",messagesX);
	WritePrivateProfileString("Settings","messagesX",str,iniFile);

	str.Format("%d",messagesY);
	WritePrivateProfileString("Settings","messagesY",str,iniFile);

	str.Format("%d",messagesW);
	WritePrivateProfileString("Settings","messagesW",str,iniFile);

	str.Format("%d",messagesH);
	WritePrivateProfileString("Settings","messagesH",str,iniFile);

	str.Format("%d",callsWidth0);
	WritePrivateProfileString("Settings","callsWidth0",str,iniFile);
		
	str.Format("%d",callsWidth1);
	WritePrivateProfileString("Settings","callsWidth1",str,iniFile);

	str.Format("%d",callsWidth2);
	WritePrivateProfileString("Settings","callsWidth2",str,iniFile);

	str.Format("%d",callsWidth3);
	WritePrivateProfileString("Settings","callsWidth3",str,iniFile);

	str.Format("%d",callsWidth4);
	WritePrivateProfileString("Settings","callsWidth4",str,iniFile);

	str.Format("%d",activeTab);
	WritePrivateProfileString("Settings","ActiveTab",str,iniFile);

	str.Format("%d",alwaysOnTop);
	WritePrivateProfileString("Settings","AlwaysOnTop",str,iniFile);
	
	WritePrivateProfileString("Settings","cmdIncomingCall",cmdIncomingCall,iniFile);
	WritePrivateProfileString("Settings","cmdCallAnswer",cmdCallAnswer,iniFile);
	
}
